package com.device.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;

public class JWTUtil {
    private static final String JWT_ID = "jwt";
    private final static String JWT_SECRET = "9a6H8j3sE2dR1fT7gY5hN4mW0qP";

    private static final int JWT_TTL = 24*60*60*1000;  //millisecond

    /**
     * 由字符串生成加密key
     */
    private static SecretKey generalKey(){
        byte[] encodedKey = Base64.decodeBase64(JWT_SECRET);
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
    }

    /**
     * 创建jwt
     * @param subject JSON字符串
     */
    public static String createJWT(String subject) {
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        Date exp = new Date(nowMillis + JWT_TTL);
        SecretKey key = generalKey();
        JwtBuilder builder = Jwts.builder()
                .setId(JWT_ID)
                .setIssuedAt(now)
                .setSubject(subject)
                .setExpiration(exp)
                .signWith(SignatureAlgorithm.HS256, key);
        return builder.compact();
    }

    /**
     * 解密jwt
     */
    public static Object parseJWT(String jwt) {
        SecretKey key = generalKey();
        Claims claims = Jwts.parser()
                .setSigningKey(key)
                .parseClaimsJws(jwt)
                .getBody();
        return claims.get("sub");
    }

}
